package com.thssh.ui.other.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.thssh.ui.R;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private ListView listView;

    private List<String> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        initData();
        listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return mData.size();
            }

            @Override
            public Object getItem(int position) {
                return mData.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Log.d("ListViewActivity", "getView-position: " + position);
                ViewHolder holder = null;
                if(convertView == null){
                    holder = new ViewHolder();
                    convertView = View.inflate(ListViewActivity.this, android.R.layout.simple_list_item_1, null);
                    holder.tv = convertView.findViewById(android.R.id.text1);
                    convertView.setTag(holder);
                }else{
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tv.setText(mData.get(position));
                return convertView;
            }
        });
    }

    static class ViewHolder{
        public TextView tv;
    }

    private void initData() {
        mData = new ArrayList<>();
        for(int i = 0; i< 100; i++){
            mData.add("data-" + i);
        }
    }
}
